package com.li.core.controller.hikaricp;

import com.alibaba.fastjson.JSONObject;
import com.li.configserve.config.datasource.HikariCpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @description: HikariCp测试
 * @author: Lidong
 * @time: 2020/6/21 16:57
 **/
@RequestMapping("/hikar")
@RestController
public class HikariCpTestController {

    private static final Logger logger = LoggerFactory.getLogger(HikariCpTestController.class);

    @Autowired
    HikariCpConfig hikariCpConfig ;


    @RequestMapping("/test1")
    public String test1() throws SQLException {
        Connection connection = hikariCpConfig.connection();
        Statement statement = connection.createStatement();
        try {
            ResultSet resultSet = statement.executeQuery("select * from t_menu");
            String string = null;
            while (resultSet.next()) {
                logger.info(JSONObject.toJSONString(resultSet.getRow()));
                for (int i = 0; i < resultSet.getRow(); i++) {
                    String string1 = resultSet.toString();
                    logger.info(string1);
                }
                string = resultSet.getString(1);
            }
            statement.close();
            return string;
        }catch (Exception e){
            connection.close();
            return null;
        }finally {
            statement.close();
            connection.close();
        }
    }

}
